/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package aimozg.joglfw;

import javax.media.opengl.GL;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;


/**
 *
 * @author aimozg
 */
public class GLColor implements GLRenderable{
    public final double red;
    public final double green;
    public final double blue;
    public final double alpha;

    public short byteRed(){
        return (short)(red*256);
    }
    public short byteGreen(){
        return (short)(green*256);
    }
    public short byteBlue(){
        return (short)(blue*256);
    }
    public String hex(){
        return String.format("%02x%02x%02x",byteRed(),byteGreen(),byteBlue());
    }
    
    public GLColor(GLColor other){
        this(other.red,other.green,other.blue,other.alpha);
    }
    
    public GLColor(String s){
        GLColor c = null;
        int hex = 0;
        try{
            c = (GLColor)(GLColor.class.getField(s.toUpperCase()).get(null));
        }catch (Exception ex){
            try{
                hex = Integer.parseInt(s, 16);
            } catch (NumberFormatException exx){
                throw new IllegalArgumentException(String.format("Bad color string %s",s));
            }
        }
        if (c != null){
            red = c.red;
            blue = c.blue;
            green = c.green;
            alpha = c.alpha;            
        } else {
            red = (hex>>16)/256.0;
            green = ((hex>>8)&0xff)/256.0;
            blue = (hex&0xff)/256.0;
            alpha = 1.0;
        }
    }
    
    public GLColor(double red, double green, double blue, double alpha) {
        this.red = red;
        this.green = green;
        this.blue = blue;
        this.alpha = alpha;
    }

    public GLColor(double red, double green, double blue) {
        this.red = red;
        this.green = green;
        this.blue = blue;
        this.alpha = 1.0;
    }

    public void render(GL gl) {
        gl.glColor4d(red,green,blue,alpha);
    }

    public static final GLColor TRANSPARENT = new GLColor(0,0,0,0);
    public static final GLColor BLACK = new GLColor(0,0,0);
    public static final GLColor BLUE = new GLColor(0,0,1);
    public static final GLColor GREEN = new GLColor(0,1,0);
    public static final GLColor CYAN = new GLColor(0,1,1);
    public static final GLColor RED = new GLColor(1,0,0);
    public static final GLColor MAGENTA = new GLColor(1,0,1);
    public static final GLColor YELLOW = new GLColor(1,1,0);
    public static final GLColor WHITE = new GLColor(1,1,1);
    public static final GLColor LIGHTGRAY = new GLColor(0.75,0.75,0.75);
    public static final GLColor GRAY = new GLColor(0.5,0.5,0.5);
    public static final GLColor DARKGRAY = new GLColor(0.25,0.25,0.25);
    public static final GLColor NAVY = new GLColor(0,0,0.5);
    public static final GLColor DARKGREEN = new GLColor(0,0.5,0);
    public static final GLColor TEAL = new GLColor(0,0.5,0.5);
    public static final GLColor DARKRED = new GLColor(0.5,0,0);
    public static final GLColor PURPLE = new GLColor(0.5,0,0.5);
    public static final GLColor OLIVE = new GLColor(0.5,0.5,0);
    public static final GLColor ORANGE = new GLColor(1,0.5,0);
    public static final GLColor LIME = new GLColor(0.5,1,0);
    public static final GLColor BROWN = new GLColor(0.5,0.25,0);
	public static final Map<String,GLColor> constantColors;
	static{
		HashMap<String,GLColor> map = new HashMap<String,GLColor>();
		for(Field field : GLColor.class.getDeclaredFields()){
			if (Modifier.isStatic(field.getModifiers()) && field.getType() == GLColor.class){
				try {
					map.put(field.getName(),(GLColor)field.get(null));
				} catch (IllegalAccessException e) {
					e.printStackTrace();
				}
			}
		}
		constantColors = map;
	}
}
